package com.googlecode.bip.xml.export;

import java.sql.SQLException;
import java.util.List;
import javax.naming.NamingException;

/**
 * @author Claude CHOISNET
 *
 */
//not public
class XMLTopicFactory
    extends XMLSurveyEntryFactoryBuilder<XMLTopic>
{
    //private static final long serialVersionUID = 1L;

    protected XMLTopicFactory(
            XMLSurveyConnection connection,
            String              sql
            )
    {
        super( connection, sql );
    }

    public static List<XMLTopic> createList(
            XMLSurveyConnection connection,
            int                 parentidtopic
            )
        throws SQLException, NamingException
    {
        StringBuilder sql = new StringBuilder();
        sql.append( "SELECT * FROM `topics` WHERE `parentidtopic`" );

        if( parentidtopic == 0 ) {
            sql.append( " IS NULL;" );
            }
        else {
            sql.append( '=' ).append( Integer.toString( parentidtopic ) ).append( ';' );
            }
        XMLTopicFactory xml = new XMLTopicFactory( connection, sql.toString() );
        //ResultSet rset = xml.execute();
        XMLSurveyConnection.ResultSetCache rset = xml.execute(
                "idtopic",
                "order",
                "isactive"
                );

        try {
            while( rset.next() ) {
                int idtopic     = rset.getInt( "idtopic" );
                int order       = rset.getInt( "order" );
                int isactive    = rset.getInt( "isactive" );

                xml.add( new XMLTopic( connection, idtopic, order, isactive ) );
                }
            }
        finally {
            rset.close();
            }

        return xml.getList();
    }

}
